package com.example.dbsample.model.http;

import com.example.dbsample.model.enums.ResultCodeEnum;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class ResultCode<T> {
    @SerializedName(value="resultCode", alternate = {"retCode", "code"})
    private String resultCode;

    @SerializedName(value="resultMessage", alternate = {"retMsg", "message"})
    private String resultMessage;

    private T data;

    public ResultCode() {

    }
    public ResultCode(String resultCode, String resultMessage) {
        this.resultCode = resultCode;
        this.resultMessage = resultMessage;

    }
    public ResultCode(String resultCode, String resultMessage, T data) {
        this.resultCode = resultCode;
        this.resultMessage = resultMessage;
        this.data = data;
    }

    public ResultCode<T> success(T data) {
        ResultCode<T> success = success();
        success.setData(data);
        return success;
    }

    public ResultCode<T> success() {
        return new ResultCode<>(ResultCodeEnum.SUCCESS.getCode(), "成功");
    }

    public ResultCode<T> fail(ResultCodeEnum codeEnum) {
        return new ResultCode<>(codeEnum.getCode(), codeEnum.getMsg());
    }

    public ResultCode<T> fail(ResultCodeEnum codeEnum, T data) {
        return new ResultCode<>(codeEnum.getCode(), codeEnum.getMsg(), data);
    }

    public ResultCode<T> fail(String resultCode, String reslutMessage) {
        return new ResultCode<>(resultCode, reslutMessage);
    }

    public ResultCode<T> fail(String resultCode, String reslutMessage, T data) {
        return new ResultCode<>(resultCode, reslutMessage, data);
    }

    public static <T> ResultCode<T> failStatic(String code, String reslutMessage) {
        ResultCode<T> resultCode = new ResultCode();
        resultCode.setResultCode(code);
        resultCode.setResultMessage(reslutMessage);
        return resultCode;
    }
}
